﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.IO;
using System.Windows;
using System.Drawing;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using System.Threading;
using Methodes.WordTemplate1;
using WordTemplate1.Objets;
using WordTemplate1.Formulaire;
using WordTemplate1.WPF;

namespace WordTemplate1
{
    /// <summary>
    /// Ruban personnalisé du document
    /// </summary>
    public partial class RibbonTFSSpec
    {
        private void RibbonTFSSpec_Load(object sender, RibbonUIEventArgs e)
        {

        }

        /// <summary>
        /// Permet la connexion au server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void brnConexion_Click(object sender, RibbonControlEventArgs e)
        {
            TFS tfs = new TFS();
            Globals.ThisDocument.taskPanel.cbxQueries.Text = string.Empty;

            if (Globals.ThisDocument.ReadDocumentProperty("Query") != null)
            {
                var LinqResult = (from q in Globals.ThisDocument.taskPanel.ListeQuery
                                 where q.Name == Globals.ThisDocument.ReadDocumentProperty("Query")
                                 select q).FirstOrDefault();

                if (LinqResult != null)
                {
                    Globals.ThisDocument.taskPanel.cbxQueries.Text = Globals.ThisDocument.ReadDocumentProperty("Query");

                    tfs.ActualiserWI(false, LinqResult);
                }
                else
                    tfs.ActualiserWI(false);
            }
            else
            {
                tfs.ActualiserWI(false);
            }

            List<WorkItemType> lwt = tfs.GetWorkitemType();
            cbbTypeWorkitem.Items.Clear();

            if (lwt != null)
            {
                foreach (WorkItemType wt in lwt)
                {
                    RibbonDropDownItem button1 = this.Factory.CreateRibbonDropDownItem();
                    button1.Label = wt.Name;
                    button1.Tag = wt;
                    button1.SuperTip = wt.Description;

                    cbbTypeWorkitem.Items.Add(button1);
                }

                if (TFS.tfsTP != null)
                {
                    RibbonTFSSpec re = (RibbonTFSSpec)Globals.Ribbons.GetRibbon(typeof(RibbonTFSSpec));

                    re.btnAddModele.Enabled = true;
                    re.btnInsert.Enabled = true;
                    re.btnPublish.Enabled = true;
                    re.btnRefrech.Enabled = true;
                    cbbTypeWorkitem.Enabled = true;
                    re.cbxSynchro.Enabled = true;
                    re.btnCreate.Enabled = true;
                    re.btnIteration.Enabled = true;

                    Globals.ThisDocument.taskPanel.ActiveCompoment(true);

                    txtProjet.Text = TFS.project.Name;
                    txtServer.Text = TFS.tfsTP.Name;
                    txtProjet.ScreenTip = TFS.project.Name;
                    txtServer.ScreenTip = TFS.tfsTP.Name;

                }
            }
        }

        /// <summary>
        /// Permet de raffraichir les workitems depuis les nouveauté sur le serveur
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefrech_Click(object sender, RibbonControlEventArgs e)
        {
            Globals.ThisDocument.taskPanel.Actualise();
        }

        /// <summary>
        /// Permet d'ajouter un modéle de bloc dans le document
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddModele_Click(object sender, RibbonControlEventArgs e)
        {
            frmModele m = new frmModele();
            m.ShowDialog();
        }

        /// <summary>
        /// Permet de publier sur le serveur les modifications des éléments
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPublish_Click(object sender, RibbonControlEventArgs e)
        {
            var elements = from search in Globals.ThisDocument.taskPanel.ListElement
                           where search.IsModify == true
                           && search.Type == TypeElement.Field
                           select search;

            foreach (EField element in elements)
            {
                element.Save();
            }

            Globals.ThisDocument.taskInformation.ActualiseInformation();
        }

        /// <summary>
        /// Permet d'insérer un bloc d'instruction
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInsert_Click(object sender, RibbonControlEventArgs e)
        {
            Contents.AddContentBlock();
        }

        private void btnCreate_Click(object sender, RibbonControlEventArgs e)
        {
            var LinqResult = from search in cbbTypeWorkitem.Items
                             where search.Label == cbbTypeWorkitem.Text
                             select search;

            if (LinqResult.Count() > 0)
            {
                TFS tfs = new TFS();
                tfs.CreateWI((WorkItemType)LinqResult.First().Tag);
            }
        }

        private void btnIteration_Click(object sender, RibbonControlEventArgs e)
        {
            using (AreaPathIterationForm frm = new AreaPathIterationForm())
            {
                frm.ShowDialog();
            }
        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            Form1 wf = new Form1();
            wf.ShowDialog();
        }


    }
}

